﻿using Pigeon.Utility;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Xml;

namespace QD.Snapshot.Core.Config
{
    public class SiteConfig
    {
        public static int UserId { get; set; }

        public static int Page { get; set; }
        public static int PageSize { get; set; }
        /// <summary>
        /// 机器终端号
        /// </summary>
        public static int ClientId { get; set; }
        public static int AgentId { get; set; }
        public static string ApiDomain { get; set; }
        /// <summary>
        /// 数据库字符串
        /// </summary>
        public static string ConnectionString { get; set; }
        /// <summary>
        /// 文件生成目录
        /// </summary>
        public static string HtmlPath { get; set; }
        /// <summary>
        /// 可用的搜索引擎
        /// </summary>
        public static string Engine { get; set; }
        /// <summary>
        /// 截图间隔周期,默认24小时
        /// </summary>
        public static int Cycle { get; set; }

        /// <summary>
        /// 消息通知的用户
        /// </summary>
        public static string NotifyToUser { get; set; }

        public static string IP { get; set; }
        public static void Init()
        {
            loadIniFile();  //加载终端号
        }

        static void loadIniFile()
        {
            TIniFile ini = new TIniFile(AppDomain.CurrentDomain.BaseDirectory + "Config.ini");

            SiteConfig.UserId = ini.ReadInt("Snapshot", "UserId", 0);       //2013-10-22 wanghui 截图的UserId
            SiteConfig.AgentId = ini.ReadInt("Snapshot", "AgentID", 0);
            SiteConfig.ClientId = ini.ReadInt("Snapshot", "ClientId", 0);  //2013-12-26 添加标识每台电脑的编号
            SiteConfig.ApiDomain = ini.ReadString("Snapshot", "ServerUrl", "api.711688.com.cn");//api.711688.com.cn
            SiteConfig.PageSize = ini.ReadInt("Snapshot", "PageSize", 20);
            SiteConfig.Engine = ini.ReadString("Snapshot", "Engine", "百度,360,搜狗,bing,中国搜索,微信");
            SiteConfig.Cycle = ini.ReadInt("Snapshot", "Cycle", 24);     //截图周期，默认24小时
            SiteConfig.HtmlPath = ini.ReadString("Snapshot", "HtmlPath", AppDomain.CurrentDomain.BaseDirectory);
            SiteConfig.Engine = SiteConfig.Engine.ToLower();

            SiteConfig.Page = ini.ReadInt("Snapshot", "CurrentPage", 1);       //2018-08-05 第几页
            //2017-08-08
            SiteConfig.ConnectionString = ini.ReadString("Snapshot", "ConnectionString", "Data Source=114.55.133.202,2434;Initial Catalog=sql_s002;Persist Security Info=True;User ID=yin;Password=yin1016");

            SiteConfig.NotifyToUser = ini.ReadString("Snapshot", "NotifyToUser", "");       //消息通知用户
            SiteConfig.IP = GetLocalIP();
        }

        public static string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }

            }
            catch { }
            return "";
        }
    }
}